package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.adapter.MyAdapter;
import com.lenovo.manufacture.annotation.Layout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import butterknife.BindView;

public class Module8 extends BaseActivity {

    @BindView(R.id.rv_module8)
    RecyclerView rvModule8;
    @BindView(R.id.tv_info)
    TextView tvInfo;

    List<Map<String, String>> carStores;
    List<Map<String, String>> cars;
    Map<String, String> stringMap = new HashMap<>();
    int size = 0;
    String capacity = "";

    @Layout(value = R.layout.activity_module8, title = "成品车辆查询")
    protected void init() {
        async(apiService.userCarStore(), data -> {
            carStores = data.getData();
            async(apiService.getCarType(), data1 -> {
                cars = data1.getData();
                async(apiService.getAllUserLine(), data2 -> {
                    for (Map<String, String> userLine : data2.getData()) {
                        stringMap.put(userLine.get("id"), userLine.get("userLineName"));
                    }
                    initList();
                    getInfo();
                });
            });
        });
    }

    private void initList() {
        List<String> strings = new ArrayList<>();
        carStores.forEach(carStore -> {
            Map<String, String> car1 = cars.stream().filter(car -> car.get("id").equals(carStore.get("carTypeId"))).collect(Collectors.toList()).get(0);
            strings.add(String.valueOf(carStore.get("id")));
            strings.add(car1.get("carTypeName"));
            strings.add(carStore.get("num"));
            strings.add(car1.get("size"));
            strings.add(stringMap.get(carStore.get("userLineId")));
            String s = carStore.get("type").equals("0") ? "正常" : "维修";
            strings.add(s);
        });
        rvModule8.setLayoutManager(new GridLayoutManager(this, 6));
        rvModule8.setAdapter(new MyAdapter(strings));
    }

    @SuppressLint("DefaultLocale")
    private void getInfo() {
        for (Map<String, String> carStore : carStores) {
            for (Map<String, String> car : cars) {
                if (carStore.get("carTypeId").equals(car.get("id"))) {
                    size += Integer.valueOf(car.get("size")) * Integer.valueOf(carStore.get("num"));
                    break;
                }
            }
        }
        json.put("id", 1);
        async(apiService.getInfo(json), data -> {
            capacity = data.getData().get(0).get("capacity2");
            tvInfo.setText(String.format("成品车辆库存信息：共占地%d，库存容量%s", size, capacity));
        });
    }
}
